package com.xbl.mail.service.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xbl.mail.constant.Const;
import com.xbl.mail.emumeration.MailStatus;
import com.xbl.mail.entity.MailSend;
import com.xbl.mail.service.MailSendService;
import com.xbl.mail.utils.KeyUtil;

@RestController
public class ProducerController {
	private static Logger LOGGER = LoggerFactory.getLogger(ProducerController.class);
	
	@Autowired
	MailSendService mailSendService;
	
	//send
	@RequestMapping(value="/send",produces={"application/json"})
	public String send(@RequestBody(required=true) MailSend mailSend) throws Exception{
		//LOGGER.info("id:{},to:{}",mailSend.getSendId(),mailSend.getSendTo());
		//TODO 数据的较验
		mailSend.setSendId(KeyUtil.generatorUUID());
		mailSend.setSendCount(0L);
		mailSend.setSendStatus(MailStatus.DRAFT.getCode());
		mailSend.setVersion(0L);
		mailSend.setUpdateBy(Const.SYS_RUNTIME);
		//把发送邮件的数据存到数据库中
		mailSendService.insert(mailSend);
		//把数据投递到redis中,并且更新数据库表记录的状态 
		mailSendService.sendRedis(mailSend);
		return "{}";
	}
}
